import Dialog from '../../dist/dialog/dialog.js';
import server from "../../utils/server.js"
var openid = wx.getStorageSync("openId");
const token = wx.getStorageSync('token');
Page({
  data: {
    name: "",
    //price:"￥250.00",
    tabs2: [1, 2],
    "background": [
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    time: 24 * 60 * 60 * 1000,
    timeData: {},
    dataRenderingItem: {},
    name: '',
    type: '',
    activityTip: '距结束还剩:'
  },

  onChange(e) {
    this.setData({
      timeData: e.detail
    });
  },

  onToHome: function (event) {
    wx.switchTab({
      url: "/pages/home/index/index",
    })
  },

  onToCheckout: function (event) {
    let _this = this
    wx.navigateTo({
      url: "/pages/checkout/checkout",

      // 向结款页面传值(id)
      success(res) {
        console.log(_this.data.type)
        res.eventChannel.emit('productToCheckOut', {
          data: {
            id: _this.data.dataRenderingItem.id,
            type: _this.data.type
          }
        })
      }

    })
  },


  // 跳转到顶部
  onToTop: function (e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },


  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    //console.log('123')
    wx.showLoading({
      title: '加载中',
    })

    // 获取上个页面传递来的数据(id)
    let _this = this
    const eventChannel1 = this.getOpenerEventChannel()
    eventChannel1.on('checkOutToProduct', function (data) {
      console.log("123")
      console.log(data)
      _this.text(data.data.id),
      _this.setData({
          type: data.data.type
      })
    })

    // 获取上个页面传递来的数据(id)
    const eventChannel2 = this.getOpenerEventChannel()
    eventChannel2.on('awardListToProduct', function (data) {
      console.log('awardListToProduct')
      console.log(data.data.id)
      console.log("data.data.type:" + data.data.type)
      console.log(data.data)
      _this.text(data.data.id)
      _this.setData({
        type: data.data.type
      })
    })

  },

  //轮播图片
  text: function (id) {
    var that = this;
    wx.request({
      header:{
        "Authorization": token
      },
      url: 'http://localhost:8080/goods/selectWxGoodsById/' + id,
      success(res) {
        console.log(res.data.data)
        //console.log(res.data.data.wxImageList);
        let imageList = res.data.data.wxImageVoList;

        let backgroundList = [];
        for (let index = 0; index < imageList.length; index++) {
          backgroundList.push(imageList[index].imageUrl);
        }

        backgroundList = backgroundList.map(el => {
          return server + el;
        })

        let dataRenderingItem = res.data.data
        //console.log("dataRenderingItem")
        //console.log(dataRenderingItem)

        // 商品详情部分
        //console.log(dataRenderingItem.wxGoodsAttrList)
        let goodsDetail = [];
        for (let index = 0; index < dataRenderingItem.wxGoodsAttrVoList.length; index++) {
          goodsDetail.push(dataRenderingItem.wxGoodsAttrVoList[index])
          //console.log(goodsDetail)
        }



        that.setData({
          background: backgroundList,
          dataRenderingItem,
          detailLlist: goodsDetail

        })
        //console.log(backgroundList)

        wx.hideLoading();//隐藏加载
      }
    })
  },

})